var app = getApp();
const http = app.http;
const util = app.util;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hasmore: true,
    page: 1,
    type: 1,
    statistics: [],
    year: '',
    month: '',
    isLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var time = util.formatTime(new Date());
    var t = time.substr(0, 7);
    var year = time.substr(0, 4)
    var month = time.substr(5, 2)
    this.setData({
      month: year + '-' + month
    })
    this.loadlist(1, 1, this.data.month)
  },
  loadlist: function(page, type, month) {
    var that = this;
    http.$ajax({
      url: 'v2.taskmoney/getTaskMoneyList',
      data: {
        page: page,
        month: month,
        type: type,
        row:15,
      }
    }, {
      success: (res) => {
        var data = res.data;
        var hasmore = true;
        var page = that.data.page;
        var datalist = data.list.data;
        datalist.forEach(v => {
          v.category = app.processImg(v.category);
        });
        that.data.list.push(...datalist);
        if (page == 1) {
          var statistics = data.statistics;
          that.setData({ statistics: statistics })
        }    
        if (datalist.length < 15) {
          hasmore = false;
        }
        that.setData({
          hasmore: hasmore,
          list: that.data.list,
          page: page,
          month: month,
          isLoading: false
        });
      }
    })
  },
  bindTimeChange: function(e) {
    var date = e.detail.value
    this.setData({
      month: date,
      list: [],
      hasmore: true,
      statistics: [],
      page:1
    })
    this.loadlist(1, this.data.type, date)
  },
  daiqueren: function () {
    this.setData({
      type: 1,
      list: [],
      hasmore: true
    })
    this.loadlist(1, 1, this.data.month)
  },
  wancheng: function () {
    this.setData({
      type: 2,
      list: [],
      hasmore: true
    })
    this.loadlist(1, 2, this.data.month)

  },
  
  gotoRate() {
    wx.navigateTo({
      url: '/pages/rate/geshui',
    })
  },
  //时间
  bindDateChange(e){
    let theDate = e.detail.value;
    this.setData({
      date: theDate
    })
  },
  binderrorimg(event) {
    let index = event.target.dataset.index;
    var img = 'list[' + index + '].photo_path'
    this.setData({
      [img]: '/images/zhanweitu.png'
    })   
  }, 
  //跳转到众保费确认
  gotoConfirm(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/rate/confirm?id=' + id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    this.data.list = [];
    that.loadlist(1, that.data.type, that.data.month);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    this.setData({
      isLoading: true
    });
    if (that.data.hasmore) {
      var page = that.data.page;
      page += 1;
      that.setData({ page: page })
      that.loadlist(page, that.data.type,that.data.month);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})